<template>
  <div>
    <svg-icon v-if="isIcon"
      class="icon-color"
      :icon-class="isFullscreen ? 'exit-fullscreen' : 'fullscreen'"
      color="currentColor"
      @click="click"
    />
    <div v-else><span v-if="isFullscreen"  @click="click">缩小</span><span v-else  @click="click">全屏</span></div>
  </div>
</template>

<script>
import screenfull from "screenfull";

export default {
  name: "ScreenFull",
  props: {
    isIcon: {
      type: Boolean,
      default: true
    },
    ele: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isFullscreen: false,
    };
  },
  mounted() {
    this.init();
  },
  beforeDestroy() {
    this.destroy();
  },
  methods: {
    click() {
      if (!screenfull.isEnabled) {
        this.$message({ message: "你的浏览器不支持全屏", type: "warning" });
        return false;
      }
      console.log(this.$refs[this.ele])
      if (this.ele) {
        screenfull.toggle(this.$refs[this.ele]);
      } else {
        screenfull.toggle();
      }

    },
    change() {
      this.isFullscreen = screenfull.isFullscreen;
    },
    init() {
      if (screenfull.isEnabled) {
        screenfull.on("change", this.change);
      }
    },
    destroy() {
      if (screenfull.isEnabled) {
        screenfull.off("change", this.change);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.icon-color {
  width: 20px;
  height: 16px;
}

</style>
